const BASE_URL = getApp().globalData.BASE_URL
const app = getApp()
let token = null
let request_list = []
let is_login = false
// 登录获取token处理
const login = cb => {
        // 记录所有请求
        request_list.push(cb)
        // 防止重复请求登录
        if (!is_login) {
          is_login = true;
        } else {
          return
        }
      // 刷新令牌
      // wx.login({
      //   success: function (res) {
          request({
            url: '/cms/user/refresh',
            method: 'GET',
            header: {
              Authorization: "Bearer"+wx.getStorageSync('refresh_token') || ''
            }
          }).then(res => {
            if (res.statusCode == 200 ) {
              token = res.access_token;
              wx.setStorageSync('access_token', res.data.access_token)
              wx.setStorageSync('refresh_token', res.data.refresh_token)
              // 全部请求回调
              request_list.forEach(cb => {
                cb && cb()
              })
              request_list = []; 
              is_login = false;
            }else{
              wx.setStorageSync('refresh_token', '')
              wx.navigateTo({
                url: '/pages/login/index',
              })
            }
          })
      //   }
      // })
}

// 统一请求封装
const request = obj => {
  return new Promise((resolve, reject) => {
    wx.showLoading({
      title:'加载中...'
    }) 
    //带请求token
    // if (token) {
    //   obj.data = obj.data || {}
    //   obj.data.access_token = token;
    // }
    // console.log(getApp().globalData.access_token )
    wx.request({
      url: BASE_URL + obj.url,
      data: obj.data,
      header: {
        Authorization: "Bearer "+wx.getStorageSync('access_token') || ''
      },
      method: obj.method || 'POST',
      success: function (res) {
        console.log('res:',res)
        wx.hideLoading()
        if(res.data.request=='POST /cms/user/login'&&res.data.code==10021){
          wx.showToast({
            icon:'none',
            title: res.data.message,
          })
          reject()
        }else if (res.statusCode == 201) {
          wx.showToast({
            title: res.data.message,
          })
          resolve(res);
        }else if (res.statusCode == 404 || res.statusCode == 400) {
          wx.showToast({
            icon:'none',
            title: res.data.message,
          })
        }else if (res.statusCode != 200) {
          // 请求错误
          // reject()
           if (res.data.code == 10041 || res.data.code == 10042 || res.data.code == 10050 || res.data.code == 10051 || res.data.code == 10052 || res.data.code==10013) {
            //用户身份过期
            login(() => {
              request(obj).then(res=>{
                resolve(res)
              })
            })
          }else if(res.data.code!=10041){
            wx.showToast({
              icon:'none',
              title: res.data.message,
            })
          } else {
            reject()
          }
        }else{
          resolve(res);
        }
      },
      fail: function (err) {
        reject(err)
        wx.hideLoading()
      },
      complete: function (res) {
      },
    })
  })
}

export default request